package com.ld.security.server.admin.modules.gl.util;

import com.ld.security.server.admin.modules.gl.entity.GlEnumEntity;
import com.ld.security.server.admin.modules.gl.entity.GlEnumItemEntity;
import com.ld.security.server.admin.modules.gl.po.EnumDetailsPo;
import com.ld.security.server.admin.modules.gl.po.EnumSavePo;
import com.ld.security.server.admin.modules.gl.service.IGlEnumService;
import com.ld.security.server.admin.modules.gl.vo.EnumDetailsVo;
import com.ld.security.common.core.constant.CSuperConstant;
import com.ld.security.common.core.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 枚举工具
 */
@Component
public class EnumUtil {

   private static IGlEnumService enumService;

    /**
     * 华为枚举实体
     * @param enSuperEnum
     * @param typeName
     * @return
     */
   public static EnumSavePo toEnumEntity(Class enSuperEnum, String typeName){
       ApiException.throw400(!enSuperEnum.isEnum(),"异常枚举类"+enSuperEnum);

       EnumSavePo po =new EnumSavePo();
       GlEnumEntity p =  new GlEnumEntity();
       p.setCode(enSuperEnum.getSimpleName());
       p.setLabel(p.getCode());
       p.setTypeName(typeName);
       po.setParent(p);
       for (Object o : enSuperEnum.getEnumConstants()) {
           CSuperConstant.EnSuperEnum e=(  CSuperConstant.EnSuperEnum)o;
           GlEnumItemEntity item =   new GlEnumItemEntity();
           item.setLabel(e.getLabel());
           item.setValue(e.getValue());
           po.addChildren(item);
       }
       return po;

   }

    /**
     * 添加一个枚举
     * @param mockEntity
     * @return
     */
    public static int save(EnumSavePo mockEntity) {
        enumService.saveEnum(mockEntity);
        return 1;
    }

    /**
     * 获得一个枚举
     * @param code
     * @return
     */
    public static EnumDetailsVo findByCode(String code) {

        EnumDetailsVo vo = enumService.getEnumDetails(code);
        return vo;
    }


    @Autowired
    public void setEnumService(IGlEnumService enumService) {
        EnumUtil.enumService = enumService;
    }
}
